
$(function(){

	/*
	if(typeof(sort_name) != "undefined"){
		cur_sort = sort_name[sort_js];
		$("#show_sort").html(cur_sort + '<em></em>');
	}
	*/
	//分类显示
	
	$("#show_categories").click(function(){		
		showArea("navbox");
		$(this).parents("li").addClass("cur");

		return false;
	});
	$("#show_district").click(function(){		
		showArea("navbox_district");
		$(this).parents("li").addClass("cur");
		return false;
	});
	$("#show_sort").click(function(){		
		showArea("navbox_sort");
		$(this).parents("li").addClass("cur");
		return false;
	});
	$("#fullbg").click(function(){
		hideArea("navbox");
		hideArea("navbox_district");
		hideArea("navbox_sort");
		return false;
	});
	$("#fullbg_f").click(function(){
		show_float();
		return false;
	});
	
	$(".cf li a").each(function(){
		if($(this).attr("class") == "cur"){
			$("#cate_3_a").addClass("down");
		}
	})
	
	
	
	/*//图片延迟加载
	$(".pic img").lazyload({
		effect : "fadeIn",
		threshold : 200
	});
	*/
	 //图片延迟另一种方法实现
    //var dynload_img_offset = $(".dynload").offset().top;
	//$(window).scroll(function(){
	//	dynload_img(dynload_img_offset);
	//})
	//dynload_img(dynload_img_offset);
	/*
	$("#cate_3 .cat2_css a").each(function(){
		if($(this).attr("class") == "cur"){
			if($(this).text() != '全部'){
				cur_cat_name = cat2_name = $(this).text();
				return false;
			}
		}
	})
	*/

	//商圈地铁切换

	/*$("#district_button").click(function(){
		$(".nav-ors li").removeClass("selected");
		$(this).parents("li").addClass("selected");
		$("#subway_show").hide();
		$("#district_show").show();
		return false;
	});
	$("#subway_button").click(function(){
		$(".nav-ors li").removeClass("selected");
		$(this).parents("li").addClass("selected");
		$("#district_show").hide();
		$("#subway_show").show();
		return false;
	});
	
	$(".tit").click(function(){
		var cla = $(this).attr("class");
		if(cla.indexOf("click")>=0){
				
			 var pos = $(this).offset().top-47;
			 $("html,body").scrollTop(pos);
		}
	    return false;
	});
	

    $(".plusbtn").click(function () {
    	show_float();
    });

    $('#fullbg_f').bind("touchmove", function(e){
    	hide_float();
    });
    
    //banner条
    var pauseTime = 5000;//停顿时间 毫秒
	var banner_t = banner_n = 0, banner_count; 	
	banner_count=$("#banner_list li").length; 

	$("#banner_list a:not(:first-child)").hide(); 
	$("#banner span").click(function() { 
		
		var i = $(this).attr("picCount") - 1;
		banner_n = i; 
		
		if (i >= banner_count) return; 
		$("#banner_list li").filter(":visible").fadeOut(500).parent().children().eq(i).fadeIn(1000); 
		
		//$("#banner_list").animate({left: "+=400"});
		
		$(this).addClass("cur").siblings().removeClass("cur"); 
	}); 
	
	if(banner_count>1){
		banner_t = setInterval(function showAuto() 
		{ 
			banner_n = banner_n >=(banner_count - 1) ? 0 : ++banner_n; 
			$("#banner span").eq(banner_n).trigger('click'); 
		} , pauseTime);
	}
	$("#banner").hover(function(){clearInterval(banner_t)}, function(){
		banner_t = setInterval(function showAuto() 
		{ 
			banner_n = banner_n >=(banner_count - 1) ? 0 : ++banner_n; 
			$("#banner span").eq(banner_n).trigger('click'); 
		} , pauseTime); 
		
	}); 
	
	$(".viewclose").click(function(){
		$(".banner-view").hide();
		clearInterval(banner_t);
	});
	
	if("undefined" != typeof _gaq){
		$("a").click(function(){
	
		
			var h = $(this).attr("href");
			var t = $(this).text();
			
			if(h.indexOf("index.php") != "-1" ){
				
				var sort = h.substring(h.indexOf("sort=")+5);
						
				if($("#navbox").is(":visible")){
					var mCate = "全部生活";
					
					var cat_2 = t.substring(0,t.indexOf("("));
					var pcat = 0;
					var cat_1 = "";
					if(h.indexOf("pcat=")>0){
						pcat = h.substring(h.indexOf("pcat=")+5,h.indexOf("&sort"));
						var pname = $("#cate_"+pcat+"_a").text();
						cat_1 = pname.substring(0,pname.indexOf("("));	
					}
					if(!cat_1){
						pcat = h.substring(h.indexOf("cat=")+4,h.indexOf("&"));
						var pname = $("#cate_"+pcat+"_a").text();
						cat_1 = pname.substring(0,pname.indexOf("("));
						
						
					}	
					if(""==cat_2){
						cat_2 = "全部";
					}
					if(""==cat_1){
						cat_1 = cat_2;
					}			
					_gaq.push(['_trackEvent', mCate, city_name, cat_1+'|'+cat_2, 1]);
					
				}else if($("#navbox_sort").is(":visible")){
					var mCate = "默认排序";
					_gaq.push(['_trackEvent', mCate, city_name, t, 1]);
				}
				
				
				
			}
			 
			
		});
	}
    if("distance" == sort_js){
    	
    	//initPosInfo();
    	
    }*/

})

function show_float() {

	$(".circle").toggle();
	if ($(".plusbtn i").hasClass("iconplus")) {
		$(".plusbtn i").removeClass("iconplus");
	} else {
		$(".plusbtn i").addClass("iconplus")
	}
	$('#fullbg_f').toggle();
}
function hide_float() {

	$(".circle").hide();
	if ($(".plusbtn i").hasClass("iconplus")) {
		$(".plusbtn i").removeClass("iconplus");
	}
	$('#fullbg_f').hide();
}
function show_cate2(cate_id,event){
	
	$("#navbox dd a").removeClass("down");
	


	//$('#cate_'+cate_id).toggle();
	$(".category-box").each(function(){
		if($(this).attr("id") == "cate_"+cate_id){
			var cla = $('#'+$(this).attr("id")+"_a").attr("class");
			
			if(cla.indexOf("click")<0)
				$('#'+$(this).attr("id")+"_a").attr("class",cla+" click");
			else
				$('#'+$(this).attr("id")+"_a").attr("class",cla.substr(0,cla.indexOf("click")));
			
			$(this).toggle();
		}else{
			var cla = $('#'+$(this).attr("id")+"_a").attr("class");

			if(cla.indexOf("click") >= 0)
				$('#'+$(this).attr("id")+"_a").attr("class",cla.substr(0,cla.indexOf("click")));
			
			$(this).hide();
		}
		
	})
	$("#cate_"+cate_id+"_a").addClass("down");
	resetHeight();
	
}
function show_cate3(cate_id){
	$('.cf').hide();
	$(".category-box h3 a").removeClass("cur");
	$(".category-box h3 a").removeClass("cur_1");
	$("#cate_"+cate_id+"_a").addClass("cur");
	$('#cate_'+cate_id).toggle();
	resetHeight();
}
function show_zone(district_id){	
	$("#navbox_district dd a").removeClass("down");
	
	$(".category-box").each(function(){
		if($(this).attr("id") == "district_"+district_id){
			var cla = $('#'+$(this).attr("id")+"_a").attr("class");
			
			if(cla.indexOf("click")<0)
				$('#'+$(this).attr("id")+"_a").attr("class",cla+" click");
			else
				$('#'+$(this).attr("id")+"_a").attr("class",cla.substr(0,cla.indexOf("click")));
			
			$(this).toggle();
		}else{
			var cla = $('#'+$(this).attr("id")+"_a").attr("class");

			if(cla.indexOf("click") >= 0)
				$('#'+$(this).attr("id")+"_a").attr("class",cla.substr(0,cla.indexOf("click")));
			
			$(this).hide();
		}
		
	})
	$('#district_'+district_id+"_a").addClass("down");
	resetHeight();
}
function show_zhan(line_id){	
	$("#navbox_district dd a").removeClass("down");
	
	$(".category-box").each(function(){
		if($(this).attr("id") == "line_"+line_id){
			var cla = $('#'+$(this).attr("id")+"_a").attr("class");
			
			if(cla.indexOf("click")<0)
				$('#'+$(this).attr("id")+"_a").attr("class",cla+" click");
			else
				$('#'+$(this).attr("id")+"_a").attr("class",cla.substr(0,cla.indexOf("click")));
			
			$(this).toggle();
		}else{
			var cla = $('#'+$(this).attr("id")+"_a").attr("class");

			if(cla.indexOf("click") >= 0)
				$('#'+$(this).attr("id")+"_a").attr("class",cla.substr(0,cla.indexOf("click")));
			
			$(this).hide();
		}
		
	})
	$('#line_'+line_id+"_a").addClass("down");
	resetHeight();
}

function showArea(areaName){
	
	/*setTimeout(function() {
		$('#'+areaName).css( {
			"-webkit-transition" : "-webkit-transform 0.4s",
			"transition" : "-webkit-transform 0.4s",
			"-webkit-transform-origin" : "0px 0px",
			"-webkit-backface-visibility" : "hidden",
			"-webkit-transform-style" : " preserve-3d",
			"-webkit-transform" : "translate(0, 0)"
		});
	}
	,1);*/
	$('#'+areaName).show();
	$('#fullbg').show();
	
	resetHeight();
}
function hideArea(areaName){
	$('#'+areaName).removeAttr("style");

	/*$('#'+areaName).css( {
		"-webkit-transition" : "-webkit-transform 0.4s",
		"transition" : "-webkit-transform 0.4s",
		"-webkit-transform-origin" : "0px 0px",
		"-webkit-backface-visibility" : "hidden",
		"-webkit-transform-style" : " preserve-3d",
		"-webkit-transform" : "translate(280, 0)"
	});*/
	$('#'+areaName).hide();

	$('#fullbg').hide();
	$('#'+areaName).removeAttr("style");
	$(".mall-cate li").removeClass("cur");
}

function resetHeight(){
	//页面高度随菜单高度变化
	var cate_height = $("#navbox_inner").height();
	
	var district_height = $("#navbox_district_inner").height();
	
	var min_height = cate_height;
	if(cate_height < district_height)
		min_height = district_height;
	
	var v3_height = parseInt($(".v3").css("height"));

	if(min_height > v3_height){
		$(".v3").css("height",min_height+'px');
	}
}


function dynload_img(dynload_img_offset){
	var dynload_obj = $(".dynload");
	dynload_obj.each(function(i,vlaue){
	    var dynload_offset_first = $(".dynload").offset().top;
		var cur_dynload_offset = $(this).offset().top;
		if(dynload_offset_first > dynload_img_offset){
			offset = cur_dynload_offset - $(document).scrollTop();
		}else{
			offset = cur_dynload_offset;
		}
        if($(this).attr("title") != '' && (offset < ($(document).scrollTop() + $(window).height()))){
        	$(this).attr("src", $(this).attr("title"));
        	$(this).removeAttr("title");
        }
    })
}
/* 地理位置信息初始化 暂不用
function initPosInfo(){
	
	var cookie_lat = getCookie("cur_lat");
	var cookie_lng = getCookie("cur_lng");
	
	if(cookie_lat>0 && cookie_lng>0){
		
	}else{
		if(navigator.geolocation){
			navigator.geolocation.getCurrentPosition(function(position) {
				cur_lat = position.coords.latitude;
				cur_lng = position.coords.longitude;
				
				var exp  = new Date();    
			    exp.setTime(exp.getTime() + 10*60*1000);
			    document.cookie = "cur_lat="+ escape (cur_lat) + ";expires=" + exp.toGMTString()+";domain=.lashou.com";
			    document.cookie = "cur_lng="+ escape (cur_lng) + ";expires=" + exp.toGMTString()+";domain=.lashou.com";
			    
			},function(error){
				switch(error.code){  
					case error.PERMISSION_DENIED:  
						alert("用户选择了拒绝了位置服务");  
						break;  
					case error.POSITION_UNAVAILABLE:  
						alert("位置不可知");  
						break;  
					case error.TIMEOUT:  
						alert("无法获得位置信息");  
						break;  
				} 
				window.location.href = "/index.php";
				
			},{maximumAge:3000,timeout:5000,enableHighAccuracy:true});
		//Try Google Gears Geolocation
		}else{
			alert("浏览器不支持位置服务");
			window.location.href = "/index.php";
	  	}
	}
	
	
}
*/
function getDistanceList(s_url,booking){
	
	var cookie_lat = getCookie("cur_lat");
	var cookie_lng = getCookie("cur_lng");
	$("#distance_a").text("正在获取位置信息...");
	
	if(cookie_lat>0 && cookie_lng>0){
		doPos(s_url,booking);
	}else{
		if(navigator.geolocation){
			navigator.geolocation.getCurrentPosition(function(position) {
				cur_lat = position.coords.latitude;
				cur_lng = position.coords.longitude;
		
				$("#distance_a").text('成功获取,正在跳转...');
				var exp  = new Date();    
			    exp.setTime(exp.getTime() + 10*60*1000);
			    document.cookie = "cur_lat="+ escape (cur_lat) + ";expires=" + exp.toGMTString()+";domain=.v513.com";
			    document.cookie = "cur_lng="+ escape (cur_lng) + ";expires=" + exp.toGMTString()+";domain=.v513.com";
			    
			    //document.cookie = "cur_lat="+ escape (cur_lat) + ";expires=" + exp.toGMTString()+";";
			    //document.cookie = "cur_lng="+ escape (cur_lng) + ";expires=" + exp.toGMTString()+";";
				
				doPos(s_url,booking);
			},function(error){
				switch(error.code){  
					case error.PERMISSION_DENIED:   
						$("#distance_a").text('您选择了拒绝位置服务');
						setTimeout('hideAllArea()',1000);
						break;  
					case error.POSITION_UNAVAILABLE:  
						$("#distance_a").text('位置信息获取失败');
						setTimeout('hideAllArea()',1000);
						break;  
					case error.TIMEOUT:  
						$("#distance_a").text('位置信息获取失败');
						setTimeout('hideAllArea()',1000);
						break;  
				} 
				
			},{maximumAge:3000,timeout:5000,enableHighAccuracy:true});
		//Try Google Gears Geolocation
		}else{
			$("#distance_a").text('获取失败');
			setTimeout('hideAllArea()',1000);
	  	}
	}
	
}
function hideAllArea(){
	hideArea("navbox");
	hideArea("navbox_district");
	hideArea("navbox_sort");
	$("#distance_a").text('离我最近');
}

function doPos(s_url,booking,cur_lat,cur_lng){
	if(booking){
		var redirect_url = s_url+'&sort=distance&b=1';
	}else{
		var redirect_url = s_url+'&sort=distance';
	}
	
	window.location.href = redirect_url;
	
}
function getCookie(name){
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}